var mid = '';
var keyid = '';
var dialogType = '';
function MenuHtml(num,data) {
    var MenuHtml = '<li class="jsMenu pre_menu_item grid_item jslevel1 ui-sortable ui-sortable-disabled size1of' + num + ' current selected" id="'+data.mid+'_menu_' + data.keyid + '" onclick="setPreMenu(this)"><a href="javascript:void(0);" class="pre_menu_link" draggable="false"><i class="icon_menu_dot js_icon_menu_dot dn" style="display: none;"></i> <i class="icon20_common sort_gray"></i><span class="js_l1Title">菜单名称</span></a><div class="sub_pre_menu_box js_l2TitleBox" style=""> <ul class="sub_pre_menu_list"><li class="js_addMenuBox" onclick="addSubMenu(this)"><a href="javascript:void(0);" class="jsSubView js_addL2Btn" title="最多添加5个子菜单" draggable="false"><span class="sub_pre_menu_inner js_sub_pre_menu_inner"><i class="icon14_menu_add"></i></span></a></li></ul><i class="arrow arrow_out"></i> <i class="arrow arrow_in"></i></div></li>';
    return MenuHtml;
}
function setSubMenuHtml(data) {
    var subMenuHtml = '<li id="'+data.mid+'_subMenu_'+data.keyid+'" class="jslevel2 selected current" onclick="setSubMenu(this)"><a href="javascript:void(0);" class="jsSubView" draggable="false"><span class="sub_pre_menu_inner js_sub_pre_menu_inner"><i class="icon20_common sort_gray"></i><span class="js_l2Title">子菜单名称</span></span></a></li>';
    return subMenuHtml;
}
function setAddMenuHtml(num) {
    var addMenuHtml = '<li class="js_addMenuBox pre_menu_item grid_item no_extra size1of' + num + '" onclick="addPreMenu()"> <a href="javascript:void(0);" class="pre_menu_link js_addL1Btn" title="最多添加3个一级菜单" draggable="false"><i class="icon14_menu_add"></i></a> </li>';
    return addMenuHtml;
}
function addPreMenu() {
    var menuList = document.getElementById('menuList');
    var firstLi = menuList.querySelectorAll('.pre_menu_item');
    var Html = '';
    var uid = getCookie("userid");
    var timeS = new Date().getTime().toString();
    var menuName = $(".js_menu_name");
    timeS = timeS.substring(4, timeS.length);
    var urlText = $("#urlText");
    var postData = {
        uid: uid,
        name: '菜单名称',
        type: 0,
        keyid: timeS,
        parentsKeyid: 0,
        param: ""
    }
    $.ajax({
        url: wxconfigurl + "wxmenu/",
        data: JSON.stringify(postData),
        type: "POST",
        contentType: "application/json; charset=utf-8", //编码
        dataType: 'json',
        success: function (data) {
            mid = data.mid;
            keyid = data.keyid;
            switch (firstLi.length) {
                case 1:
                    $(menuList).removeClass('no_menu');
                    Html = MenuHtml(2,data) + setAddMenuHtml(2);
                    $(menuList).html(Html);
                    break;
                case 2:
                    $(firstLi).removeClass('size1of2').addClass('size1of3');
                    firstLi[0].querySelector('.sub_pre_menu_box').style.display = "none";
                    $(firstLi).removeClass('current selected');
                    Html = (firstLi[0].outerHTML + MenuHtml(3,data) + setAddMenuHtml(3));
                    menuList.innerHTML = Html;
                    break;
                case 3:
                    for (var i = 0; i < firstLi.length - 1; i++) {
                        firstLi[i].querySelector('.sub_pre_menu_box').style.display = "none";
                    }
                    $(firstLi).removeClass('current selected');
                    Html = (firstLi[0].outerHTML + firstLi[1].outerHTML + MenuHtml(3,data) + setAddMenuHtml(0));
                    menuList.innerHTML = Html;
                    break;
            }
            menuName.val(data.name);
            rightBoxInit(0);
            urlText.val('');
        },
        error: function (er) {
            alert("添加菜单失败!!!")
        }
    });
}
function setPreMenu(dom) {
    var menuList = document.getElementById('menuList');
    var firstLi = menuList.querySelectorAll('.pre_menu_item');
    var subPreMenuBox = $('.sub_pre_menu_box');
    var allSubLi = menuList.querySelectorAll('.jslevel2');
    var childSubLi = dom.querySelectorAll('.jslevel2');
    var menuName = $(".js_menu_name");
    var urlText = $("#urlText");
    var menuIdArr = dom.id.split("_menu_");
    var jsNone = document.getElementById('js_none'),jsRightBox = document.getElementById('js_rightBox');
    mid = menuIdArr[0];
    keyid = menuIdArr[1];
    $.ajax({
        url: wxconfigurl + "wxmenu/" + mid,
        type: "GET",
        success: function (data) {
            console.log(data);
            $(firstLi).removeClass('current selected');
            $(allSubLi).removeClass("current selected");
            subPreMenuBox.css({display: "none"});
            $(dom).addClass('current selected');
            $(dom).find('.sub_pre_menu_box').css({display: "block"});
            menuName.val(data.name);
            urlText.val(data.param);
            jsRightBox.style.display = "block";
            jsNone.style.display = "none";
            rightBoxInit(childSubLi.length);
        },
        error: function (er) {

        }
    });
}
function addSubMenu(dom) {
    event.stopPropagation();
    var parentUl = dom.parentNode;
    var subLi = parentUl.querySelectorAll('li');
    var parentMid = mid;
    var subMenuHtml = '';
    var menuList = document.getElementById('menuList');
    var firstLi = menuList.querySelectorAll('.pre_menu_item');
    var parentMenu = parentUl.parentNode.parentNode;
    var parentKeyid = parentMenu.id.split("_menu_")[1];
    var uid = getCookie("userid");
    var menuName = $(".js_menu_name");
    var timeS = new Date().getTime().toString();
    var urlText = $("#urlText");
    timeS = timeS.substring(4, timeS.length);
    var subMenu = {
        uid: uid,
        name: '子菜单名称',
        type: 0,
        keyid: timeS,
        parentsKeyid: parentKeyid,
        param: ""
    };
    if(urlText.val()!==''&&subLi.length==1){
        $(".dialog_wrp").css({display:"block"});
        $(".mask").css({display:"block"});
        dialogType = '添加子菜单';
        document.getElementById('deleteDialog').style.display = 'none';
        document.getElementById('subMenuDialog').style.display = 'block';
    }else{
        $.ajax({
            url: wxconfigurl + "wxmenu/",
            data: JSON.stringify(subMenu),
            type: "POST",
            contentType: "application/json; charset=utf-8", //编码
            dataType: 'json',
            success: function (data) {
                mid = data.mid;
                keyid = data.keyid;
                rightBoxInit(0);
                if(subLi.length==1){
                    parentMenu.querySelector('.icon_menu_dot').removeAttribute('style','display:none');
                }
                for (var i = 0; i < subLi.length; i++) {
                    if (i == subLi.length - 1) {
                        subMenuHtml += setSubMenuHtml(data);
                    } else {
                        subMenuHtml += subLi[i].outerHTML;
                    }
                    parentUl.innerHTML = subMenuHtml + dom.outerHTML;
                }
                subLi = parentUl.querySelectorAll('li');
                for (var j = 0; j < subLi.length; j++) {
                    if (j < subLi.length - 2) {
                        $(subLi[j]).removeClass('current selected');
                    } else {
                        break;
                    }
                }
                if (subLi.length == 6) {
                    $(subLi[5]).css({display: "none"});
                }
                $(firstLi).removeClass("current selected");
                menuName.val(data.name);
                urlText.val(data.param);
            },
            error: function (er) {
                alert("添加子菜单失败！！")
            }
        });
    }
}
function setSubMenu(dom) {
    event.stopPropagation();
    var parentUl = dom.parentNode;
    var subLi = $(parentUl).find('.jslevel2');
    var menuList = document.getElementById('menuList');
    var firstLi = menuList.querySelectorAll('.pre_menu_item');
    var jsNone = document.getElementById('js_none'),jsRightBox = document.getElementById('js_rightBox');
    var menuName = $(".js_menu_name");
    var menuIdArr = dom.id.split("_subMenu_");
    var urlText = $("#urlText");
    mid = menuIdArr[0];
    keyid = menuIdArr[1];
    rightBoxInit(0); $.ajax({
        url: wxconfigurl + "wxmenu/" + mid,
        type: "GET",
        success: function (data) {
            menuName.val(data.name);
            urlText.val(data.param);
            $(firstLi).removeClass("current selected");
            subLi.removeClass("current selected");
            $(dom).addClass("current selected");
            jsRightBox.style.display = "block";
            jsNone.style.display = "none";
        },
        error: function (er) {

        }
    });
}

function setMenuByMid() {
    var submenuId = mid + "_subMenu_" + keyid;
    var premenuId = mid + "_menu_" + keyid;
    $.ajax({
        url: wxconfigurl + "wxmenu/" + mid,
        data: JSON.stringify({uid: getCookie("uid"), name: $(".js_menu_name").val()}),
        type: "POST",
        contentType: "application/json; charset=utf-8", //编码
        dataType: 'json',
        success: function (data) {
            if (document.getElementById(premenuId)) {
                document.getElementById(premenuId).querySelector(".js_l1Title").innerHTML = data.name
            }
            else {
                document.getElementById(submenuId).querySelector(".js_l2Title").innerHTML = data.name
            }
            //$("#"+menuId).find(".js_l2Title").html(data.name);//jq修改
            //document.getElementById(submenuId).querySelector(".js_l2Title").innerHTML = data.name;//js修改
        },
        error: function (er) {
            alert("修改失败")
        }
    });
}

//设置初始样式
function menuInit(menuLIst) {
    var menuList = document.getElementById('menuList');
    var menuLen = menuLIst.length;
    var menuLi, menuLiHtml = '';
    var menuName = $(".js_menu_name");
    var urlText = $("#urlText");
    if (menuLIst.length != 0){
        if (menuLen >= 3) {
            menuLi = 3;
        } else {
            menuLi = menuLen + 1;
        }
        for (var i = 0; i < menuLen; i++) {
            menuLiHtml += '<li class="jsMenu pre_menu_item grid_item jslevel1 ui-sortable ui-sortable-disabled size1of' + menuLi;
            if (i == 0 && menuLIst[0].wxMenuList.length == 0) {
                menuLiHtml += ' current selected';
                mid = menuLIst[0].mid;
                keyid = menuLIst[0].keyid;
                menuName.val(menuLIst[0].name);
                switch (menuLIst[0].type){
                    case 1:
                        urlText.val(menuLIst[0].param);
                        break;
                }
            }
            menuLiHtml += '" id="' + menuLIst[i].mid + '_menu_' + menuLIst[i].keyid + '" onclick="setPreMenu(this)"><a href="javascript:void(0);" class="pre_menu_link" draggable="false"><i class="icon_menu_dot js_icon_menu_dot dn"';
            if (menuLIst[i].wxMenuList.length == 0) {
                menuLiHtml += ' style="display: none;"';
            }
            menuLiHtml += '></i> <i class="icon20_common sort_gray"></i><span class="js_l1Title">' + menuLIst[i].name + '</span></a><div class="sub_pre_menu_box js_l2TitleBox" ';
            if (i > 0) {
                menuLiHtml += 'style="display:none"';
            }
            menuLiHtml += '><ul class="sub_pre_menu_list">';
            var subMenuLi = menuLIst[i].wxMenuList;
            for (var j = 0; j < subMenuLi.length; j++) {
                menuLiHtml += '<li id="' + subMenuLi[j].mid + '_subMenu_' + subMenuLi[j].keyid + '" class="jslevel2';
                if (i==0&&j == 0) {
                    menuLiHtml += ' current selected';
                    mid = subMenuLi[j].mid;
                    keyid = subMenuLi[j].keyid;
                    menuName.val(subMenuLi[j].name);
                    switch (subMenuLi[j].type){
                        case 1:
                            urlText.val(subMenuLi[j].param);
                            break;
                    }
                }
                menuLiHtml += '" onclick="setSubMenu(this)"><a href="javascript:void(0);" class="jsSubView" draggable="false"><span class="sub_pre_menu_inner js_sub_pre_menu_inner"><i class="icon20_common sort_gray"></i><span class="js_l2Title">' + subMenuLi[j].name + '</span></span></a></li>'
            }
            menuLiHtml += '<li class="js_addMenuBox" onclick="addSubMenu(this)"';

            if (subMenuLi.length >= 5) {
                menuLiHtml += ' style="display: none"';
            }
            menuLiHtml += '><a href="javascript:void(0);" class="jsSubView js_addL2Btn" title="最多添加5个子菜单" draggable="false"><span class="sub_pre_menu_inner js_sub_pre_menu_inner"><i class="icon14_menu_add"></i></span></a></li>';
            menuLiHtml += '</ul><i class="arrow arrow_out"></i> <i class="arrow arrow_in"></i></div></li>';
        }
        menuLiHtml += setAddMenuHtml(menuLi);
        $(menuList).removeClass('no_menu');
        menuList.innerHTML = menuLiHtml;
    }
}
//删除菜单
function delMenu(){
    $(".dialog_wrp").css({display:"block"});
    $(".mask").css({display:"block"});
    dialogType = '删除菜单';
    document.getElementById('deleteDialog').style.display = 'block';
    document.getElementById('subMenuDialog').style.display = 'none';
}
function cancelDialog(){
    $(".dialog_wrp").css({display:"none"});
    $(".mask").css({display:"none"});
}
function sureClick(){
    if(dialogType == '删除菜单'){
        var subMenuId = mid + "_subMenu_" + keyid;
        var preMenuId = mid + "_menu_" + keyid;
        var subMid,subKeyid;
        var subMenuDom = document.getElementById(subMenuId);
        var preMenuDom = document.getElementById(preMenuId);
        var menuIdArr,success,subMenuSuccess;
        var subPreMenuBox = $(".sub_pre_menu_box");
        if(preMenuDom){
            var subMenuLi = preMenuDom.querySelectorAll(".jslevel2");
            var nowSubMenuNum = 0;
            success = function(){
                nowSubMenuNum++
                if(nowSubMenuNum==subMenuLi.length){
                    cancelDialog();
                    ajaxDel(preMenuDom);
                }
            }
            if(subMenuLi.length!=0){
                for(var i=0;i<subMenuLi.length;i++){
                    menuIdArr = subMenuLi[i].id.split("_subMenu_")
                    subMid = menuIdArr[0];
                    subKeyid = menuIdArr[1];
                    subMenuDom = document.getElementById(subMenuLi[i].id);
                    ajaxDel(subMenuDom,success,subMid);
                }
            }else{
                ajaxDel(preMenuDom);
            }
        }else{
            subMenuSuccess = function(){
                var parentUl = subMenuDom.parentNode;
                var subMenuLi = parentUl.querySelectorAll('li');
                var subMenuAddLi = parentUl.querySelector('.js_addMenuBox');
                var parentMenu = parentUl.parentNode.parentNode;
                if(subMenuLi.length == 6){
                    subMenuAddLi.style.display = "block";
                }else if(subMenuLi.length == 2){
                    parentMenu.querySelector('.icon_menu_dot').style.display = "none";
                }
                cancelDialog();
            }
            ajaxDel(subMenuDom,subMenuSuccess);
        }
        subPreMenuBox.css({display:"none"});
    }else if(dialogType == '添加子菜单'){
        $.ajax({
        url: wxconfigurl + "wxmenu/" + mid,
        data: JSON.stringify({param:'',type:0}),
        type: "POST",
        contentType: "application/json; charset=utf-8", //编码
        dataType: 'json',
        success: function (data) {
            var timeS = new Date().getTime().toString();
            var urlText = $("#urlText");
            var subMenuHtml = '';
            timeS = timeS.substring(4, timeS.length);
            var menuName = $(".js_menu_name");
            var uid = getCookie("userid");
            var subMenu = {
                uid: uid,
                name: '子菜单名称',
                type: 0,
                keyid: timeS,
                parentsKeyid: keyid,
                param: ""
            };
            var parentMenu = document.getElementById(mid+'_menu_'+keyid);
            var subPreMenuList = parentMenu.querySelector('.sub_pre_menu_list');
            $.ajax({
                url: wxconfigurl + "wxmenu/",
                data: JSON.stringify(subMenu),
                type: "POST",
                contentType: "application/json; charset=utf-8", //编码
                dataType: 'json',
                success: function (data) {
                    rightBoxInit(0);
                    parentMenu.querySelector('.icon_menu_dot').removeAttribute('style','display:none');
                    subMenuHtml += setSubMenuHtml(data);
                    subPreMenuList.innerHTML = subMenuHtml + subPreMenuList.innerHTML;
                    $(parentMenu).removeClass("current selected");
                    menuName.val(data.name);
                    urlText.val(data.param);
                    mid = data.mid;
                    keyid = data.keyid;
                    cancelDialog();
                },
                error: function (er) {
                    alert("添加子菜单失败！！")
                }
            });
        },
        error: function (er) {
            alert("修改失败")
        }
    });
    }
}
function ajaxDel(menuDom,successFunc,submid){
    var DelMid;
    var jsNone = document.getElementById('js_none'),jsRightBox = document.getElementById('js_rightBox');
    if(submid){
        DelMid = submid;
    }
    else{
        DelMid = mid;
    }
    $.ajax({
        url: wxconfigurl + "wxmenu/" + DelMid,
        data: JSON.stringify({uid: getCookie("uid"), name: $(".js_menu_name").val()}),
        type: "DELETE",
        success: function (data) {
            if(successFunc){
                successFunc();
            }else{
                mid = '';
                keyid = '';
            }
            menuDom.outerHTML='';
            jsRightBox.style.display = "none";
            jsNone.style.display = "block";
        },
        error: function (er) {
            alert("删除失败！！！")
        }
    });
}
//右边栏选择菜单内容
function chooseMenuContent(self){
    var ContentType = self.getAttribute("data-editing");
    var contentSelect = self.parentNode.querySelector('.selected');
    $(contentSelect).removeClass("selected");
    $(self).addClass("selected");
    if(ContentType == 0){
        $("#edit").show();
        $("#url").hide();
    }else{
        $("#edit").hide();
        $("#url").show();
    }
}
//点击已有子菜单的一级菜单右边样式
function rightBoxInit(subMenuLen){
    var innerNone = document.getElementById('js_innerNone');
    var setContentBox = document.querySelector('.js_setContentBox');
    var menuContentContainer = document.querySelector('.menu_content_container');
    if(subMenuLen==0){
        innerNone.style.display = "none";
        setContentBox.style.display = "block";
        menuContentContainer.style.display = "block";
    }else{
        innerNone.style.display = "block";
        setContentBox.style.display = "none";
        menuContentContainer.style.display = "none";
    }
}
//设置菜单链接
function updateMenuUrl(self){
    var uid = getCookie("userid");
    var postData = {
        uid: uid,
        type: 1,
        param: self.value
    }
    $.ajax({
        url: wxconfigurl + "wxmenu/" + mid,
        data: JSON.stringify(postData),
        type: "POST",
        contentType: "application/json; charset=utf-8", //编码
        dataType: 'json',
        success: function (data) {
            console.log(data);
        },
        error: function (er) {
            alert("修改失败")
        }
    });
}
//发送消息，消息类型切换
function sendMessChange(){
    var messTypeList = $(".tab_navs_wrp").find('li');
    var lastType = $(".tab_navs_wrp").find('.selected');
    var tabContentList = $(".tab_panel").find(".tab_content");
    for(var i=0;i<messTypeList.length;i++){
        messTypeList[i].onclick = function(){
            var type = parseInt(this.getAttribute('data-type'));
            lastType.removeClass("selected");
            $(this).addClass("selected");
            lastType = $(this);
            switch (type){
                case 10:
                    tabContentList.css({display:"none"});
                    tabContentList[0].style.display = "block";
                    break;
                case 2:
                    tabContentList.css({display:"none"});
                    tabContentList[1].style.display = "block";
                    break;
                case 3:
                    tabContentList.css({display:"none"});
                    tabContentList[2].style.display = "block";
                    break;
                case 15:
                    tabContentList.css({display:"none"});
                    tabContentList[3].style.display = "block";
                    break;
            }
        }
    }
}
//注册页面判断
function checkPhoneIsExist() {
    return true;
}
/*按钮倒计时*/
var timer;
var countdown = 60
function settime() {
    if (countdown == 0) {
        document.getElementById("getcode").removeAttribute("disabled");
        document.getElementById("getcode").value = "获取验证码";
        $("#getcode").css({"background-color": "#0c79b1"})
        clearTimeout(timer);
        countdown = 60
        return;
    }
    document.getElementById("getcode").setAttribute("disabled", true);
    document.getElementById("getcode").value = "重新发送(" + countdown + ")";
    countdown = countdown - 1;
    $("#getcode").css({"background-color": "#cccccc"})
    timer = setTimeout("settime()", 1000);
}
//cookie设置读取和删除
//设置
function setCookie(name, value, day) {
    var Days = day;
    var exp = new Date();
    exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
    document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}
//读取
function getCookie(name) {
    var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
    if (arr = document.cookie.match(reg)) {
        return unescape(arr[2]);
    }
    else {
        return null;
    }
}
//删除
function delCookie(name) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var cval = getCookie(name);
    if (cval != null) {
        document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
    }
}